package com.ltkj.baseplatform.service.impl;

import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.ltkj.baseplatform.domain.ArtContent;
import com.ltkj.baseplatform.domain.bo.ArtContentBo;
import com.ltkj.baseplatform.service.IArtContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ltkj.baseplatform.mapper.WuserCollectMapper;
import com.ltkj.baseplatform.domain.WuserCollect;
import com.ltkj.baseplatform.service.IWuserCollectService;

/**
 * 用户收藏Service业务层处理
 * 
 * @author chenzeen
 * @date 2023-08-31
 */
@Service
public class WuserCollectServiceImpl implements IWuserCollectService 
{
    @Autowired
    private WuserCollectMapper wuserCollectMapper;

    @Autowired
    private IArtContentService artContentService;

    /**
     * 查询用户收藏
     * 
     * @param id 用户收藏主键
     * @return 用户收藏
     */
    @Override
    public WuserCollect selectWuserCollectById(Long id)
    {
        return wuserCollectMapper.selectWuserCollectById(id);
    }

    /**
     * 查询用户收藏列表
     * 
     * @param wuserCollect 用户收藏
     * @return 用户收藏
     */
    @Override
    public List<WuserCollect> selectWuserCollectList(WuserCollect wuserCollect)
    {
        return wuserCollectMapper.selectWuserCollectList(wuserCollect);
    }

    /**
     * 新增用户收藏
     * 
     * @param wuserCollect 用户收藏
     * @return 结果
     */
    @Override
    public int insertWuserCollect(WuserCollect wuserCollect)
    {
        //收藏量+1
        if("1".equals(wuserCollect.getContentType())) {
            ArtContent content = artContentService.selectArtContentById(wuserCollect.getContentId());
            Long newNum = content.getCollectNum()+1;
            content.setCollectNum(newNum);
            ArtContentBo contentBo = new ArtContentBo();
            BeanUtil.copyProperties(content, contentBo);
            artContentService.updateArtContent(contentBo);
        }
        return wuserCollectMapper.insertWuserCollect(wuserCollect);
    }

    /**
     * 修改用户收藏
     * 
     * @param wuserCollect 用户收藏
     * @return 结果
     */
    @Override
    public int updateWuserCollect(WuserCollect wuserCollect)
    {
        return wuserCollectMapper.updateWuserCollect(wuserCollect);
    }

    /**
     * 批量删除用户收藏
     * 
     * @param ids 需要删除的用户收藏主键
     * @return 结果
     */
    @Override
    public int deleteWuserCollectByIds(Long[] ids)
    {
        return wuserCollectMapper.deleteWuserCollectByIds(ids);
    }

    /**
     * 删除用户收藏信息
     * 
     * @param id 用户收藏主键
     * @return 结果
     */
    @Override
    public int deleteWuserCollectById(Long id)
    {
        WuserCollect wuserCollect = wuserCollectMapper.selectWuserCollectById(id);
        //收藏量-1
        if(wuserCollect!= null && "1".equals(wuserCollect.getContentType())) {
            ArtContent content = artContentService.selectArtContentById(wuserCollect.getContentId());
            Long newNum = content.getCollectNum()-1;
            content.setCollectNum(newNum);
            ArtContentBo contentBo = new ArtContentBo();
            BeanUtil.copyProperties(content, contentBo);
            artContentService.updateArtContent(contentBo);
        }
        return wuserCollectMapper.deleteWuserCollectById(id);
    }
}
